import InteractionTimerService from "./service";
import style from "./css.css";

const TIMER_VIEW_TYPE_CODE = {
    PAGING_VIEW: 'type_paging',
    TEXT_VIEW: 'type_text',
    NEW_TIMER_VIEW: 'new_timer'
};
@Midway.MidwayModule({
    code: "InteractionTimer",
    template: require('./view.html'),
    style: style,
})
class InteractionTimer extends CommonClass {

    constructor() {
        super();
        this.metaInfo.name = 'InteractionTimer';
        this.metaInfo.type = ConstVariable.PresenterType.FUNCTIONAL;

        this.$view = null;
        this.timerSetting = {
            min: 0,
            sec: 0
        };
        this.type = TIMER_VIEW_TYPE_CODE.PAGING_VIEW;
        this.service = undefined;
    }

    getService() {
        if(this.service === undefined) {
            this.service = new InteractionTimerService(this);
        }
        return this.service;
    }

    $launch() {
        this.$view = $(this.View);
        this.hasInit = true;
        this._initTimerI18nView();
        if (this.Model.view_type) {
            this.type = this.Model.view_type;
        } else {
            this.type = TIMER_VIEW_TYPE_CODE.PAGING_VIEW;
        }
        this.Logger.debug("InteractionTimer view_type " + this.type);
        switch (this.type) {
            case TIMER_VIEW_TYPE_CODE.TEXT_VIEW: {
                this.$view.find(".time_info").css("display", "");
                break;
            }
            case TIMER_VIEW_TYPE_CODE.PAGING_VIEW: {
                this.$view.find(".timerContainer").css("display", "");
                break;
            }
            case TIMER_VIEW_TYPE_CODE.NEW_TIMER_VIEW: {
                this.$view.find(".time_default").css("display", "");
                break;
            }
            default: {
                this.$view.find(".timerContainer").css("display", "");
                break;
            }
        }
    }

    _setTimeValue(min, sec) {
        this.timerSetting.min = min;
        this.timerSetting.min = min;
        switch (this.type) {
            case TIMER_VIEW_TYPE_CODE.TEXT_VIEW: {
                this.$view.find('em.e1#timer-text').text(this._format(min) + ":" + this._format(sec));
                break;
            }
            case TIMER_VIEW_TYPE_CODE.PAGING_VIEW: {
                this.$view.find('span.timeNum#min').text(this._format(min));
                this.$view.find('span.timeNum#sec').text(this._format(sec));
                break;
            }
            case TIMER_VIEW_TYPE_CODE.NEW_TIMER_VIEW: {
                this.$view.find('.time_default_minutes').text(this._format(min));
                this.$view.find('.time_default_seconds').text(this._format(sec));
                break;
            }
            default: {
                this.$view.find('span.timeNum#min').text(this._format(min));
                this.$view.find('span.timeNum#sec').text(this._format(sec));
                break;
            }
        }

    }

    _format(num) {
        if (num < 10) {
            return '0' + num;
        }
        return num;
    }

    _getLanguageText(key) {
        if (this.languageKeySet == undefined || this.languageKeySet[key] == undefined)
            return "ERROR";
        return this.languageKeySet[key];
    }

    _initTimerI18nView() {
        this.$view.find('#_answertime').text(this._getLanguageText('answertime'));
        this.$view.find('#_minute').text(this._getLanguageText('minute'));
        this.$view.find('#_second').text(this._getLanguageText('second'));
    }
}